package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.AdVo;
import com.cskaoyan.service.AdminAdService;
import com.cskaoyan.service.AdminAdServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName: AdminAdServlet
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/5/30 13:33
 * @Version: V1.0
 */
@WebServlet("/admin/ad/*")
public class AdminAdServlet extends WdBaseServlet {

    AdminAdService adminAdService = new AdminAdServiceImpl();

    @SneakyThrows
    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {

        AdVo adVo = new AdVo();
        Map<String, String[]> parameterMap = request.getParameterMap();
        BeanUtils.copyProperties(adVo, parameterMap);

        BaseRespVo baseRespVo = adminAdService.list(adVo);

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {

        String s = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(s, MarketAd.class);

        if(marketAd.getLink()==null){
            return BaseRespVo.fail(401,"参数不对");
        }

        BaseRespVo baseRespVo = adminAdService.create(marketAd);

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        String s = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(s, MarketAd.class);

        BaseRespVo baseRespVo = adminAdService.update(marketAd);

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        String s = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(s, MarketAd.class);

        BaseRespVo baseRespVo = adminAdService.delete(marketAd);

        return baseRespVo;
    }

}
